#!/bin/bash
# =====================================
# 作者：老曹  QQ:1484743
# 版权声明：本程序为个人原创、开源程序，禁止用于任何违法用途，任何违法用途均与作者无关。
# 仅限学习交流，转载请注明出处。如需商用请联系作者。
# =====================================
# 宿主机硬盘SMART监控脚本（macOS版）
# 替换变量：{{HOST_ID}} {{TOKEN}}
HOST_ID={{HOST_ID}}
TOKEN={{TOKEN}}
API_URL="你的API地址/api/data/report"
DEVICE="/dev/disk0"
SMARTCTL=$(which smartctl)
if [ -z "$SMARTCTL" ]; then
  echo "未检测到smartctl，请用 brew install smartmontools 安装"
  exit 1
fi
SMART_JSON=$($SMARTCTL -a -j $DEVICE)
MODEL=$($SMARTCTL -i $DEVICE | grep 'Device Model' | awk -F: '{print $2}' | xargs)
SERIAL=$($SMARTCTL -i $DEVICE | grep 'Serial Number' | awk -F: '{print $2}' | xargs)
CAPACITY=$($SMARTCTL -i $DEVICE | grep 'User Capacity' | awk -F: '{print $2}' | grep -o '[0-9]*')
JSON_DATA=$(jq -n --arg hostId "$HOST_ID" --arg token "$TOKEN" --arg smart_json "$SMART_JSON" --arg device "$DEVICE" --arg model "$MODEL" --arg serial "$SERIAL" --arg capacity "$CAPACITY" '{hostId: ($hostId|tonumber), token: $token, smart_json: $smart_json, device: $device, model: $model, serial: $serial, capacity: ($capacity|tonumber)}')
curl -k -X POST -H "Content-Type: application/json" -d "$JSON_DATA" "$API_URL"
# 可加入launchd或crontab定时执行 